package opreation;

import book.Book;
import book.BookList;

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:此类删除书籍操作
 * Author:  LiuTao
 * Version: 1.0
 * Date:    2024-01-03  09:06
 */
public class DelOpreation implements IOpreation{
    @Override
    public void work(BookList bookList) {
        System.out.println("请输入删除图书的名字：");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();

        //如果存在，更改pos来确定删除位置
        int pos = -1;
        int currentSize = bookList.getUsedSize();
        int i = 0;
        for(; i < currentSize; i++) {
            Book book = bookList.getBook(i);
            if(book.getName().equals(name)) {
                pos = i;
                break;
            }
        }

        //判断是否在书架上存在
        if(i == currentSize) {
            System.out.println("没有你要删除的图书");
            return;
        }

        //删除所在书籍
        int j = pos;
        for(; j < currentSize-1; j++) {
            Book book = bookList.getBook(j + 1);
            bookList.setBooks(j,book);
        }

        //将最后的位置置为null
        bookList.setBooks(j,null);
        //进书架上存在的书籍减一
        bookList.setUsedSize(currentSize-1);
    }
}
